/*
 *  linmigrate - entry point of application
 *
 *  Copyright (C) 2011 by Volodymyr M. Shcherbyna <volodymyr@shcherbyna.com>
 *
 *      This file is part of linmigrate.
 *
 *  linmigrate is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  linmigrate is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with linmigrate.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <QtCore/QCoreApplication>

#include "debianhandler.h"
#include "cmdparser.h"

/**   Showing help text

    * @return void
*/
int HandleHelp(QTextStream & console)
{
    console << "Usage: linmigrate [COMMAND]...[OPTIONS]" << endl;

    console << "Commands: " << endl;
    console << "\t--help:   Shows help message and exit" << endl;
    console << "\t--export: Exports your configuration" << endl;
    console << "\t--import: Imports your configuration" << endl;

    return (0x00);
}

/**   Showing error text

    * @return void
*/
int HandleError(QTextStream & console)
{
    console << "Unknown option. Run linmigrate --help to list possible options" << endl;

    return (0xff);
}

/**   Handling import command

    * @return 0 in case of success, code error in case of failure
*/
int HandleImport(QTextStream & console, CCmdParser & parser)
{
    CDebianHandler handler(console);
    ImportOptions options = parser.getImportOptions();

    return handler.Import(options);
}

/**   Handling export command

    * @return 0 in case of success, code error in case of failure
*/
int HandleExport(QTextStream & console, CCmdParser & parser)
{
    CDebianHandler handler(console);
    ExportOptions options = parser.getExportOptions();

    return handler.Export(options);
}

/**   Entry point of application

    * @return 0 for success, or id of error
*/
int main(int argc, char *argv[])
{
    QTextStream console(stdout);
    QCoreApplication app(argc, argv);
    CCmdParser cmdParser(app.arguments());

    console << "++ LinMigrate v (0.0.0.4) - Linux Migrate tool" << endl;
    console << "(c) 2011 by Volodymyr M. Shcherbyna <volodymyr@shcherbyna.com>" << endl << endl;

    switch (cmdParser.getCommand())
    {
    case CCmdParser::CmdUnknown:
        return HandleError(console);

    case CCmdParser::CmdHelp:
        return HandleHelp(console);

    case CCmdParser::CmdExport:
        return HandleExport(console, cmdParser);

    case CCmdParser::CmdImport:
        return HandleImport(console, cmdParser);
    }

    // will never get here, but let's make compiler happy
    return (0x01);
}
